package com.red.wood.module.ircs.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.module.ircs.dao.IrcsHouseInfoDao;

@Repository
public class IrcsHouseInfoDaoImpl implements IrcsHouseInfoDao {

	private static final Log log = LogFactory.getLog(IrcsHouseInfoDaoImpl.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(Map<String, Object> param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		count = listCount(param);
		list = listPageData(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	@Override
	public List<Map<String, Object>> getDetailInfo(String houseId,String ircsId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select t1.houseName,t1.houseType,t1.houseZip,t1.houseAdd,t2.houseOfficeName,t2.houseOfficeIdType,t2.houseOfficeId,t2.houseOfficeMobile,t2.houseOfficeEmail,t2.houseOfficeTel,");
		sql.append("       (select area.area_name from t_area area where area.area_code = t1.houseProvince limit 1) as houseProvince,");
		sql.append("       (select area.area_name from t_area area where area.area_code = t1.houseCounty limit 1) as houseCounty,");
		sql.append("       (select area.area_name from t_area area where area.area_code = t1.houseCity limit 1) as houseCity");
		sql.append(" from t_ircs_houseinfo t1 left join t_ircs_houseinfo_houseofficer t2");
		sql.append(" on t1.houseId = t2.houseId where t1.houseId = '"+houseId+"' and t1.ircsId = '"+ircsId+"' limit 1");
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public List<Map<String, Object>> listPageData(Map<String, Object> param){
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel,"1")) {
			sql = getSql();
			sql.append(" and house.houseProvince = (select provinceid from provinces t where t.province = '"+userProvince+"' limit 1)");
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
		}else if(StringUtils.equals(userLevel, "2")) {
			sql = getSql();
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
		}else {
			sql.append("select * from (");
			sql.append(getSql());
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(" union all ");
			sql.append(getSql());
			sql.append(" and ircs.province != '"+userProvince+"'");
			sql.append(" and house.houseProvince = (select provinceid from provinces t where t.province = '"+userProvince+"' limit 1)");
			sql.append(getQueryConf(param));
			sql.append(") a");
		}
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		log.info(sql);
		return jdbcTemplate.queryForList(sql.toString());
	}

	public String listCount(Map<String, Object> param) {
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel,"1")) {
			sql.append("select count(*) count from (");
			sql.append(getSql());
			sql.append(" and house.houseProvince = (select provinceid from provinces t where t.province = '"+userProvince+"' limit 1)");
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(") a");
		}else if(StringUtils.equals(userLevel, "2")) {
			sql.append("select count(*) count from (");
			sql.append(getSql());
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(") a");
		}else {
			sql.append("select count(*) count from (");
			sql.append(getSql());
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(" union all ");
			sql.append(getSql());
			sql.append(" and ircs.province != '"+userProvince+"'");
			sql.append(" and house.houseProvince = (select provinceid from provinces t where t.province = '"+userProvince+"' limit 1)");
			sql.append(getQueryConf(param));
			sql.append(") a");
		}
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public StringBuffer getSql() {
		StringBuffer sql = new StringBuffer();
		sql.append("select house.ircsId,house.houseId,house.houseName,");
		sql.append("       (select area.area_name from t_area area where area.area_code = house.houseProvince limit 1) as houseProvince,");
		sql.append("       case when house.houseType = '1' then '租用' when house.houseType = '2' then '自建' when house.houseType = '999' then '其他' end as houseType,");
		sql.append("       (select houseOfficeName from t_ircs_houseinfo_houseofficer t where house.houseId = t.houseId limit 1) as houseOfficeName");
		sql.append(" from t_ircs_houseinfo house inner join t_ircs_ircsdata ircs");
		sql.append(" on house.ircsId = ircs.ircsId");
		sql.append(" where 1 = 1");
		return sql;
	}
	
	public StringBuffer getQueryConf(Map<String,Object> param) {
		String houseName = CommonUtils.valueOf(param.get("houseName"));
		String houseType = CommonUtils.valueOf(param.get("houseType"));
		String ircsId = CommonUtils.valueOf(param.get("ircsId"));
		StringBuffer queryConf = new StringBuffer();
		
		if(StringUtils.isNotBlank(ircsId)) {
			queryConf.append(" and house.ircsId='"+ircsId+"'");
		}
		if(StringUtils.isNotBlank(houseName)) {
			queryConf.append(" and house.houseName like '%"+houseName+"%'");
		}
		if(!StringUtils.equals(houseType, "all")) {
			queryConf.append(" and house.houseType='"+houseType+"'");
		}
		return queryConf;
	}
}
